#pragma once
#include <import\ix_dictimport.h>
#include <vector>
#include <string>

class Cx_DictImport :
	public Ix_DictImport
{
	X3BEGIN_CLASS_DECLARE(Cx_DictImport)
		X3DEFINE_INTERFACE_ENTRY(Ix_DictImport)
		X3END_CLASS_DECLARE()
protected:
	Cx_DictImport(void);
	virtual ~Cx_DictImport(void);
public:
	virtual BOOL Open() ;
	virtual BOOL IsEof();
	virtual void Next();
	virtual BOOL GetContent(LPWSTR *lpKey,LPWSTR *lpContent,LPWSTR *lpSample) ;
protected:
	TCHAR						m_szPath[MAX_PATH];
	std::vector<std::wstring>	m_files;
	int							m_nIdx;
	inline long ScanFiles(const wchar_t* path)
	{
		long count = 0;
#ifdef _WIN32
		WIN32_FIND_DATAW fd;
		wchar_t filename[MAX_PATH];

		lstrcpynW(filename, path, MAX_PATH);
		PathAppendW(filename, L"*.*");

		HANDLE hfind = ::FindFirstFileW(filename, &fd);
		BOOL loop = (hfind != INVALID_HANDLE_VALUE);

		while (loop )
		{
			if (fd.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
			{
				loop = ::FindNextFileW(hfind, &fd);
				continue;
			}

			if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				if (fd.cFileName[0] != L'.')
				{
					lstrcpynW(filename, path, MAX_PATH);
					PathAppendW(filename, fd.cFileName);
					PathAddBackslashW(filename);

					m_files.push_back(filename);
			
					count += ScanFiles(filename);

				}
			}
			else
			{
				lstrcpynW(filename, path, MAX_PATH);
				PathAppendW(filename, fd.cFileName);

				count++;
				m_files.push_back(filename);
			}

			loop = ::FindNextFileW(hfind, &fd);
		}
		::FindClose(hfind);
#endif

		return count;
	}
	LPWSTR Ansi2Unicode(LPCSTR lpStr,  DWORD nMaxLen)
	{
		LPWSTR lpResult = NULL;

		size_t nLen = strlen(lpStr);
		DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, lpStr, (DWORD)nLen, NULL, 0);
		
		lpResult = new WCHAR[max(dwNum, nMaxLen) + 1];
		if ( lpResult != NULL )
		{
			lpResult[0] = 0;
			MultiByteToWideChar(CP_ACP, 0, lpStr, (DWORD)nLen, lpResult, dwNum);
			lpResult[dwNum] = 0;
		}

		return lpResult;
	}
	
};
BOOL CompFileName(std::wstring file1,std::wstring file2);
void trim(std::wstring& str);